<?php

namespace App\Http\Controllers\Admin;

use App\Models\Artcat;
use App\Models\Article;
use App\Models\ArticleCommon;
use App\Models\LmColumn;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    public function index(Request $request)
    {
        $keys = $request->get('keys');
        $class_id = $request->get('class_id');
        $title = $request->get('title');
        $limit = $request->get('limit', 10);
        $model = Article::query();
        if ($class_id) {
            $model = $model->where('class_id', $class_id);
        }
        if ($this->aId != 1) {
            $model = $model->where('admin_id', $this->aId);
        }
        if ($keys) {
            $model = $model->where('title', 'like', '%' . $keys . '%');
        }
        if ($title) {
            $model = $model->where('admin_name', 'like', '%' . $title . '%');
        }
        $result = $model->orderBy('id', 'desc')->paginate($limit);
        foreach ($result as $value) {
            if ($value['thumb']) {
                $value['thumb'] = getAliOssImage($value['thumb']);
            }
        }
        return showMsg('操作成功', 200, $result);
    }

    public function detail(Request $request)
    {
        $id = $request->get('id');
        $result = Article::findOrFail($id);
        if (!$result) {
            return showMsg('信息已删除', 201, $result);
        }
        $result['desc'] = sub_string(html2text($result['content']), 0, 260);
        if ($result['thumb']) {
            $result['thumb'] = getAliOssImage($result['thumb']);
        }
        if ($result['content']) {
            $result['content'] = replaceOssImgSrc($result['content']);
        }
        return showMsg('操作成功', 200, $result);
    }

    public function beforeSave(Request $request)
    {
        $id = $request->get('id');
        $data = $request->all();
        if ($data['thumb']) {
            $data['thumb'] = delAliOssImage($data['thumb']);
        }
        if ($data['content']) {
            $data['content'] = replaceImgSrc($data['content']);
        }
        if ($id) {
            $article = Article::findOrFail($id);
            if ($article->update($data)) {
                return showMsg('更新成功', 200);
            }
        } else {
            $data['admin_id'] = $this->aId;
            //$data['admin_name'] = $this->admin['truename'];
            Article::create($data);
            return showMsg('添加成功', 200);
        }
    }

    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return showMsg('请选择操作项', 201);
        }
        if (is_array($ids)) {
            foreach ($ids as $v) {
                Article::destroy('id', $v);
            }
        } else {
            Article::destroy('id', $ids);
        }
        return showMsg('删除成功', 200);
    }


    public function common(Request $request)
    {
        $id = $request->get('id');
        $limit = $request->get('limit', 10);
        $model = ArticleCommon::query();
        $model = $model->where('article_id', $id);
        $result = $model->orderBy('id', 'desc')->paginate($limit);
        return showMsg('操作成功', 200, $result);
    }

    public function common_destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return showMsg('请选择操作项', 201);
        }
        if (is_array($ids)) {
            foreach ($ids as $v) {
                ArticleCommon::destroy('id', $v);
            }
        } else {
            ArticleCommon::destroy('id', $ids);
        }
        return showMsg('删除成功', 200);
    }

    public function setStatus(Request $request)
    {
        $id = $request->get('id');
        $re = ArticleCommon::where('id', $id)->first();
        if ($re['status'] == 1) {
            ArticleCommon::where('id', $id)->update(['status' => 0]);
        } else {
            ArticleCommon::where('id', $id)->update(['status' => 1]);
        }
        return showMsg('设置成功', 200);
    }

    public function setIndex(Request $request)
    {
        $id = $request->get('id');
        $re = Article::where('id', $id)->first();
        if ($re['is_index'] == 1) {
            Article::where('id', $id)->update(['is_index' => 0]);
        } else {
            Article::where('id', $id)->update(['is_index' => 1]);
        }
        return showMsg('设置成功', 200);
    }
}
